//
//  UIViewController+WHC_Debuger.m
//  WHC_Debuger
//
//  Created by WHC on 17/1/14.
//  Copyright © 2017年 WHC. All rights reserved.
//
//  Github <https://github.com/netyouli/WHC_Debuger>
//

#pragma clang diagnostic ignored "-Wdeprecated-declarations"

#if DEBUG && false
//#if DEBUG && true
#import "UIViewController+WHC_Debuger.h"
#import "WHC_Debuger.h"
#import <objc/runtime.h>

@implementation UIView (WHC_Debuger)

+ (void)load {
    static dispatch_once_t onceToken;
    dispatch_once(&onceToken, ^{
        Method mySetNeedsDisplay = class_getInstanceMethod(self, @selector(mySetNeedsDisplay));
        Method setNeedsDisplay = class_getInstanceMethod(self, @selector(setNeedsDisplay));
        method_exchangeImplementations(setNeedsDisplay, mySetNeedsDisplay);
    });
}

- (void)mySetNeedsDisplay {
    if (NSThread.currentThread != NSThread.mainThread) {
        dispatch_async(dispatch_get_main_queue(), ^{
            NSString * note = [NSStringFromClass(self.class) componentsSeparatedByString:@"."].lastObject;
            NSString * msg = [NSString stringWithFormat:@"%@不在主线程操作UI,危险!!",note];
            UIAlertView * alert = [[UIAlertView alloc] initWithTitle:@"WHC_Debuger" message:msg delegate:nil cancelButtonTitle:@"知道了" otherButtonTitles:nil, nil];
            [alert show];
            NSLog(@">>>>>>>>>%@<<<<<<<<<",msg);
        });
    }
    [self mySetNeedsDisplay];
}

@end

@implementation UIViewController (WHC_Debuger)

- (void)myDealloc {
    NSLog(@">>>>>>>>>>%@ 已经释放了<<<<<<<<<<",[NSStringFromClass(self.class) componentsSeparatedByString:@"."].lastObject);
    [self myDealloc];
}

+ (void)load {
    static dispatch_once_t onceToken;
    dispatch_once(&onceToken, ^{
        Method myViewWillAppear = class_getInstanceMethod(self, @selector(myViewWillAppear:));
        Method viewWillAppear = class_getInstanceMethod(self, @selector(viewWillAppear:));
        method_exchangeImplementations(viewWillAppear, myViewWillAppear);
        
        Method myDealloc = class_getInstanceMethod(self, @selector(myDealloc));
        Method dealloc = class_getInstanceMethod(self, NSSelectorFromString(@"dealloc"));
        method_exchangeImplementations(dealloc, myDealloc);
    });
}

- (BOOL)isPrivateVC {
    NSString * selfClass = NSStringFromClass(self.class);
    return [selfClass isEqualToString:@"UIAlertController"] ||
    [selfClass isEqualToString:@"_UIAlertControllerTextFieldViewController"] ||
    [selfClass isEqualToString:@"UIApplicationRotationFollowingController"] ||
    [selfClass isEqualToString:@"UIInputWindowController"];
}

- (void)myViewWillAppear:(BOOL)animated {
    if (![self isPrivateVC]) {
        UILabel * noteLabel = WHC_Debuger.share.whc_NoteLabel;
        if (noteLabel.superview) {
            [noteLabel.superview bringSubviewToFront:noteLabel];
        }
        if (WHC_Debuger.share.whc_CustomNote == nil) {
            WHC_Debuger.share.whc_CustomNote = @" ";
        }
        WHC_Debuger.share.whc_CustomNote = [NSString stringWithFormat:@"%@",[self topViewController].class];
        if (WHC_Debuger.share.whc_CustomNote) {
            NSLog(@"(WHC_Debuger)Start = %@",WHC_Debuger.share.whc_CustomNote);
        }
        noteLabel.text = [NSString stringWithFormat:@"%@..%@",WHC_Debuger.share.whc_CustomNote,[NSStringFromClass(self.class) componentsSeparatedByString:@"."].lastObject];
    }
    [self myViewWillAppear:animated];
}

- (UIViewController *)_topViewController:(UIViewController *)vc {
    if ([vc isKindOfClass:[UINavigationController class]]) {
        return [self _topViewController:[(UINavigationController *)vc topViewController]];
    } else if ([vc isKindOfClass:[UITabBarController class]]) {
        return [self _topViewController:[(UITabBarController *)vc selectedViewController]];
    } else {
        return vc;
    }
    return nil;
}
- (UIViewController *)topViewController {
    UIViewController *resultVC;
    resultVC = [self _topViewController:[[UIApplication sharedApplication].keyWindow rootViewController]];
    while (resultVC.presentedViewController) {
        resultVC = [self _topViewController:resultVC.presentedViewController];
    }
    return resultVC;
}

@end

#endif
